const app = getApp()
const $api = require('../../utils/api.js').API;

Page({
    data: {
        imgUrl:app.globalData.imgUrl,
        banner:'',
        page:1,
        totalPge:0,
        cate:{},
        course:{},
        cateId:0,
        key:0,
        totalPage:0,
        page:1,
        schoolId:0
    },
    onLoad: function(e) {
        this.setData({
          schoolId: e.id,
        })
        wx.showLoading()
        this.getBanner();
        this.getCate(e.id);
    },
    getBanner:function(){
      var that = this;
      var data = {'type':3}
      $api.get('banner',data).then(res => {
          //请求成功
          if(res.data.code == 1){
            that.setData({
              banner:res.data.data[0].img
            })
          }
      })
    },
    getCate:function(){
      var that = this;
      var data = {}
      $api.get('course/cate',data).then(res => {
          //请求成功
          if(res.data.code == 1){
            that.setData({
              cate:res.data.data,
              cateId:res.data.data[0]['id']
            })
            that.getCourse()
          }
      })
    },
    getCourse:function(){
        var that = this
        var data = {'schoolId':this.data.schoolId,'cateId':this.data.cateId,'page':this.data.page}
        var course = this.data.course
        $api.get('course/list',data).then(res =>{
            //请求成功
            if(res.data.code == 1){
              if(that.data.totalPage == 0){
                that.setData({
                  totalPage:res.data.data.last_page
                })
              }
              wx.hideLoading()
              if(that.data.page == 1){
                that.setData({
                  course:res.data.data.data
                })
              }else{
                  that.setData({
                    course:course.concat(res.data.data.data)
                  })
              }
            }
        })
    },
    info:function(e){
        var id = e.currentTarget.dataset.id
        wx.navigateTo({
            url:'/pages/course/info?id='+id
        })
    },
    select:function(e){
      var id = e.target.dataset.id;
      var key = e.target.dataset.key;
      this.setData({
        cateId:id,
        key:key,
        page:1
      })
      wx.showLoading()
      this.getCourse()
    },
    onReachBottom: function(){
      if(this.data.page < this.data.totalPage){
          //当前不是最后一页可加载
          this.setData({
              page: this.data.page + 1,
          })
          wx.showLoading()
          this.getCourse();
      }
  },
    onShareAppMessage: function() {
        return {
            title: "美业课程-天意美业直通车",
            path: "/pages/index/index?puid="+wx.getStorageSync('uid')
        };
    }
});